<?php
class ModulePrivilegeVo extends SzAbstractMySqlVo
{

    private $privilegeId;
    private $pIds;
    private $cIds;
    private $name;
    private $requestName;
    private $status;

    /**
     * Initialize ModulePrivilegeVo.
     *
     * @param $privilegeId
     * @param $pIds
     * @param $cIds
     * @param $name
     * @param $requestName
     * @param $status
     * @param boolean $isInsert default false, means for now this vo is initialized not for insert process
     * @return ModulePrivilegeVo
     */
    public function __construct($privilegeId, $pIds, $cIds, $name, $requestName, $status, $isInsert = false) {
        $this->privilegeId = $privilegeId;
        $this->pIds = json_decode($pIds, true);
        $this->cIds = json_decode($cIds, true);
        $this->name = $name;
        $this->requestName = $requestName;
        $this->status = $status;

        $this->isInsert = $isInsert;
        $this->voClassName = 'ModulePrivilegeVo';
        $this->ormName = 'ModulePrivilege';
    }

    /**
     * @see SzAbstractVo::toArray
     */
    public function toArray($needEncode = false)
    {
        return array(
            'privilegeId' => $this->getPrivilegeId(),
            'pIds' => ($needEncode) ? $this->getPIds() : $this->getRawPIds(),
            'cIds' => ($needEncode) ? $this->getCIds() : $this->getRawCIds(),
            'name' => $this->getName(),
            'requestName' => $this->getRequestName(),
            'status' => $this->getStatus(),
        );
    }

    /**
     * @see SzAbstractVo::toEntireArray
     */
    public function toEntireArray($needEncode = false)
    {
        return array(
            'privilegeId' => $this->getPrivilegeId(),
            'pIds' => ($needEncode) ? $this->getPIds() : $this->getRawPIds(),
            'cIds' => ($needEncode) ? $this->getCIds() : $this->getRawCIds(),
            'name' => $this->getName(),
            'requestName' => $this->getRequestName(),
            'status' => $this->getStatus(),
        );
    }

    /**
     * @see SzAbstractVo::toPureArray
     */
    public function toPureArray()
    {
        return array(
            $this->getPrivilegeId(),
            $this->getPIds(),
            $this->getCIds(),
            $this->getName(),
            $this->getRequestName(),
            $this->getStatus(),
        );
    }

    public function getPrivilegeId()
    {
        return $this->privilegeId;
    }

    public function setPrivilegeId($val)
    {
        $this->saveColumnStatus(0, $this->privilegeId);
        $this->privilegeId = $val;
    }

    public function getPIds()
    {
        return json_encode($this->pIds);
    }

    /**
     * Remove privilegeId from $this->pIds.
     *
     * @param int $privilegeId
     * @return void
     */
    public function removePId($privilegeId)
    {
        if (in_array($privilegeId, $this->pIds)) {
            unset($this->pIds[array_pop(array_keys($this->pIds, $privilegeId))]);
            shuffle($this->pIds);
            $this->setPIds($this->pIds);
        }
    }

    public function getRawPIds()
    {
        return $this->pIds;
    }

    public function setPIds($val)
    {
        $encodedLength = SzUtility::strLen(json_encode($val));
        if ($encodedLength > 65535) {
            SzLogger::get()->error('[ModulePrivilegeVo] The length of column pIds exceed the limit, length / limit: ' . $encodedLength . ' / 65535');
            throw new SzException(10522, array('ModulePrivilegeVo', '65535', $encodedLength));
        } else if ($encodedLength > 58981) {
            SzLogger::get()->warn('[ModulePrivilegeVo] The length of column pIds exceed the warning level, length / limit: ' . $encodedLength . ' / 65535');
        }
        $this->saveColumnStatus(1, $this->pIds);
        $this->pIds = $val;
    }

    public function getCIds()
    {
        return json_encode($this->cIds);
    }

    public function getRawCIds()
    {
        return $this->cIds;
    }

    /**
     * Add privilegeId into $this->cIds.
     *
     * @param int $privilegeId
     * @return void
     */
    public function addCIds($privilegeId)
    {
        if (!in_array($privilegeId, $this->cIds)) {
            $this->cIds[] = $privilegeId;
            $this->setCIds($this->cIds);
        }
    }

    /**
     * Remove privilegeId from $this->cIds.
     *
     * @param int $privilegeId
     * @return void
     */
    public function removeCId($privilegeId)
    {
        if (in_array($privilegeId, $this->cIds)) {
            unset($this->cIds[array_pop(array_keys($this->cIds, $privilegeId))]);
            shuffle($this->cIds);
            $this->setCIds($this->cIds);
        }
    }

    public function setCIds($val)
    {
        $encodedLength = SzUtility::strLen(json_encode($val));
        if ($encodedLength > 65535) {
            SzLogger::get()->error('[ModulePrivilegeVo] The length of column cIds exceed the limit, length / limit: ' . $encodedLength . ' / 65535');
            throw new SzException(10522, array('ModulePrivilegeVo', '65535', $encodedLength));
        } else if ($encodedLength > 58981) {
            SzLogger::get()->warn('[ModulePrivilegeVo] The length of column cIds exceed the warning level, length / limit: ' . $encodedLength . ' / 65535');
        }
        $this->saveColumnStatus(2, $this->cIds);
        $this->cIds = $val;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setName($val)
    {
        $this->saveColumnStatus(3, $this->name);
        $this->name = $val;
    }

    public function getRequestName()
    {
        return $this->requestName;
    }

    public function setRequestName($val)
    {
        $this->saveColumnStatus(4, $this->requestName);
        $this->requestName = $val;
    }

    public function getStatus()
    {
        return $this->status;
    }

    public function setStatus($val)
    {
        $this->saveColumnStatus(5, $this->status);
        $this->status = $val;
    }

}